package org.sunrise.game.rpc.example.main;

import org.sunrise.game.common.utils.Utils;
import org.sunrise.game.rpc.example.gen.CallEnum;
import org.sunrise.game.rpc.function.CallUtils;
import org.sunrise.game.rpc.function.RpcFunction;
import org.sunrise.game.rpc.node.RpcNodeManager;

import java.util.Collections;

public class RpcServerStartUp1 {
    public static void main(String[] args) throws InterruptedException {
        // 创建一个rpc节点，服务id=1
        var rpcNode = RpcNodeManager.createRpcNode(1);
        // 注册rpc函数
        CallUtils.init(rpcNode.getNodeId(), Collections.singletonList("org.sunrise.game.rpc.example.service.test2"), CallEnum.class);

        // 绑定端口
        rpcNode.start(20000);
        // 连接主服
        rpcNode.connectMaster(Utils.getLocalIpAddress(), 8000, Utils.getLocalIpAddress());

        Thread.sleep(5000);
        test();
    }

    private static void test() {
        //发起一次rpc调用
        RpcFunction rpcFunction = RpcFunction.newInstance();
        rpcFunction.call(CallEnum.TestService1_test, "id", 1);
        rpcFunction.listenResult(rpcResult -> System.out.println("getResult1 " + rpcResult.getResult()));
        //测试超时
        rpcFunction.setTimeOut(1000);

        rpcFunction.call(CallEnum.TestService1_test, "id", 10);
        rpcFunction.listenResult(rpcResult -> System.out.println("getResult2 " + rpcResult.getResult()));

        rpcFunction.call(CallEnum.TestService1_test);
        rpcFunction.listenResult(rpcResult -> {
            rpcFunction.call(CallEnum.TestService1_test, "id", 100);
            rpcFunction.listenResult(r -> System.out.println("getResult3 " + r.getResult()));
        });
    }
 }
